#!/usr/bin/env python

# pita: An invisible audio player
# Copyright (C) 2006-2007 Austin Bingham
#
# This program is free software# you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation# either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# You can reach the author at:
#     austin.bingham@gmail.com

import logging
import pita, pita.player, pita.server, pita.decoder, pita.config, pita.app

logger = logging.getLogger('pitad')

class Application(pita.app.Application):
    def __init__(self):
        pita.app.Application.__init__(self, 'pitad')

        pita.conf.add_option(
            pita.config.Option('yield_time',
                               '-y',
                               '--yield_time',
                               ['pitad', 'yield_time'],
                               0.001,
                               float,
                               help='number of seconds to sleep when yielding player thread'))
        
        pita.conf.add_option(
            pita.config.Option('driver',
                               '-d',
                               '--driver',
                               ['pitad', 'driver'],
                               '',
                               help='audio driver to use'))

    def usage(self):
        return 'usage: %prog [options]'

    def run(self, args):
        pita.decoder.driver_name = pita.conf.driver
        if pita.conf.has_section('pitad:driver_options'):
            pita.decoder.driver_options = pita.conf.items('pitad:driver_options')
        logger.info('driver options: %s' % pita.decoder.driver_options)

        p = pita.player.Player(pita.conf.yield_time)
        p.start()
        server = pita.server.Server(('', pita.conf.port), p)
        server.serve()
        p.finish()
        p.join()

if __name__ == '__main__':
    app = Application()
    app.start()
